source( "/var/www/cgi-bin/r/modules/calc_two_min_variance_portfolios.r" )

#	最小分散集合上の特別なポートフォリオを計算
#	『金融工学入門 6.9 1-ファンド定理, p209　にあるポートフォリオ
#	@param	earning_rate_means			資産の収益率の平均ベクトル
#	@param	earning_rate_covariances	資産の収益率の共分散ベクトル
#	@param	riskless_earning_rate		安全資産の収益率
#	@param	is_normalize				投資比率を正規化するかのフラグ
#	@return ２つの投資比率ベクトルを格納したリスト　$ratio, $error_message
calc_special_efficient_portfolio <- 
	function( earning_rate_means, earning_rate_covariances, riskless_earning_rate, is_normalize = TRUE )
{
	data_count <- length( earning_rate_means )

	if ( data_count != nrow( earning_rate_covariances ) ||
		 data_count != ncol( earning_rate_covariances ) )
	{
		return( list( ratio = NULL, error_message = "invalide data count" ) ) 
	}
	
	res <- calc_two_min_variance_portfolios( earning_rate_means, earning_rate_covariances, FALSE )
	if ( is.null( res$error_message ) == FALSE )
	{
		return( list( ratio= NULL, error_message = "fail to calc 2 portfolio" ) )
	}
	
	incestment_ratio = res$ratio2 - riskless_earning_rate * res$ratio1 
	if ( is_normalize ) {
		incestment_ratio = incestment_ratio / sum( incestment_ratio )
	}

	return( list( ratio = incestment_ratio, error_message = NULL ) )
}
